package com.toman.ui.operation.enterprise.attendance;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.toman.Interfaces.BaseUrl;
import com.toman.R;
import com.toman.Util.ToastUtil;
import com.toman.toolbar.ToolBarActivity;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Request;
import okhttp3.Response;

public class AttendanceActivity extends ToolBarActivity {
    PopupWindow popupWindow;

    int HourOfDay;
    int Minute;

    int HourOfDay1;
    int Minute1;
    TimePicker id_add_timePicker1;
    @BindView(R.id.tv_morning)
    TextView tvMorning;
    @BindView(R.id.tv_afternoon)
    TextView tvAfternoon;
    @BindView(R.id.tv_evening)
    TextView tvEvening;
    @BindView(R.id.btn_save)
    Button btnSave;
    private RadioGroup mGroup;
    String conttentTime;
    String conttentTime2;

    View mView;
    int an;
    TextView mShowTime;
    TextView mShowTimeStart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_attendance);
        ButterKnife.bind(this);
        initToolBar("班次管理", getResources().getColor(R.color.c1));
        initView();
        get();
    }

    private void initView() {
        mView = LayoutInflater.from(this).inflate(R.layout.tima_layout, null, false);

    }

    public void showTime(View v, final TextView textView) {
        View view = LayoutInflater.from(this).inflate(R.layout.tima_layout, null, false);
        popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);
        popupWindow.setAnimationStyle(R.style.popwin_anim_style);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = 0.5f;
        getWindow().setAttributes(lp);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = getWindow().getAttributes();
                lp.alpha = 1.0f;
                getWindow().setAttributes(lp);
            }
        });

        id_add_timePicker1 = (TimePicker) view.findViewById(R.id.id_add_timePicker1);
        id_add_timePicker1.setIs24HourView(true);

        final Button mTimeSubmit = (Button) view.findViewById(R.id.mTimeSubmit);
        mGroup = (RadioGroup) view.findViewById(R.id.mGroup);

        final RadioButton att_hd = (RadioButton) view.findViewById(R.id.att_hd);
        final RadioButton att_qs = (RadioButton) view.findViewById(R.id.att_qs);

        att_hd.setChecked(true);
        att_hd.setTextColor(getResources().getColor(R.color.white));

        mGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.att_hd:
                        //开始时间
                        att_hd.setTextColor(getResources().getColor(R.color.white));
                        att_qs.setTextColor(getResources().getColor(R.color.c1));
                        an = 1;
                        break;
                    case R.id.att_qs:
                        //结束时间
                        att_hd.setTextColor(getResources().getColor(R.color.c1));
                        att_qs.setTextColor(getResources().getColor(R.color.white));
                        an = 2;
                        HourOfDay = id_add_timePicker1.getHour();
                        Minute = id_add_timePicker1.getMinute();
                        if (HourOfDay < 10) {
                            if (Minute < 10) {
//                            mShowTime.setText();
                                conttentTime = "0" + HourOfDay + ":0" + Minute;
                            } else {
                                conttentTime = "0" + HourOfDay + ":" + Minute;
                            }
                        } else {
                            if (Minute < 10) {
                                conttentTime = HourOfDay + ":0" + Minute;
                            } else {
                                conttentTime = HourOfDay + ":" + Minute;
                            }
                        }


                        // Log.i("test", "开始时间：: "+id_add_timePicker1.getHour()+" - "+id_add_timePicker1.getMinute());

                        break;
                }
            }
        });
        mTimeSubmit.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onClick(View v) {
                if (an == 2) {
                    HourOfDay1 = id_add_timePicker1.getHour();
                    Minute1 = id_add_timePicker1.getMinute();
                    //HourOfDay = Integer.parseInt("0"+HourOfDay);
                    //  mShowTime.setText("0"+HourOfDay+":0"+Minute+"-"+"0"+HourOfDay1+":0"+Minute1);

                    if (HourOfDay1 < 10) {
                        // mShowTime.setText("0"+HourOfDay1+":0"+Minute1);
                        if (Minute1 < 10) {
                            conttentTime2 = HourOfDay1 + ":0" + Minute1;
                        } else {
                            conttentTime2 = HourOfDay1 + ":" + Minute1;

                        }
                    } else {
                        if (Minute1 < 10) {
                            conttentTime2 = HourOfDay1 + ":0" + Minute1;
                        } else {
                            conttentTime2 = HourOfDay1 + ":" + Minute1;
                        }
                    }

                    long longstr1 = Long.valueOf(conttentTime.replaceAll(":", ""));
                    long longstr2 = Long.valueOf(conttentTime2.replaceAll(":", ""));
                    if (longstr1 > longstr2) {
                        ToastUtil.makeText(getApplicationContext(), "开始时间必须小于结束时间，请重新选择后再试");

                        return;
                    }
                    textView.setText(conttentTime + "-" + conttentTime2);
                    //   Log.i("test", "结束时间：: "+HourOfDay1+" - "+Minute1+"开始时间：: "+HourOfDay+" - "+Minute);
                    popupWindow.dismiss();
                }
            }
        });
        popupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 5);
    }

    @OnClick({R.id.tv_morning, R.id.tv_afternoon, R.id.tv_evening, R.id.btn_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_morning:
                showTime(view, tvMorning);
                break;
            case R.id.tv_afternoon:
                showTime(view, tvAfternoon);
                break;
            case R.id.tv_evening:
                showTime(view, tvEvening);

                break;
            case R.id.btn_save:
                save();
                break;
        }
    }

    public void save() {
        String token = myApplication.getSp().getString("tokenId", "");
        String time;
        time = tvMorning.getText().toString() + ";" + tvAfternoon.getText().toString();
        if (!tvEvening.getText().toString().equals("")) {
            time = time + ";" + tvEvening.getText().toString();
        }

        OkHttpUtils
                .post(BaseUrl.preheat_edit)
                .params("token", token)
                .params("shift_time", time)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String result, Request request, @Nullable Response response) {
                        Log.i("test", "修改班次: " + result);
                        try {
                            JSONObject js = new JSONObject(result);
                            int code = js.getInt("code");
                            if (code == 1) {

                                Toast.makeText(getApplicationContext(), "" + js.getString("msg"), Toast.LENGTH_SHORT).show();
                                finish();
                                //customerManager();

                            } else {
                                Toast.makeText(getApplicationContext(), "" + js.getString("msg"), Toast.LENGTH_SHORT).show();


                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                });
    }

    public void get() {


        OkHttpUtils
                .post(BaseUrl.customerDesc)
                .params("token", token)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String result, Request request, @Nullable Response response) {
                        Log.i("test", "企业信息: " + result);
                        try {
                            JSONObject js = new JSONObject(result);
                            int code = js.getInt("code");
                            if (code == 1) {
                                String[] temp = null;
                                JSONObject da = js.getJSONObject("data");
                                String time = da.getString("shift_time");
                                if (!time.equals("null")) {
                                    temp = time.split(";");
                                    if (temp.length > 0) {
                                        tvMorning.setText(temp[0]);
                                    }
                                    if (temp.length >= 2) {
                                        tvAfternoon.setText(temp[1]);

                                    }
                                    if (temp.length == 3) {
                                        tvEvening.setText(temp[2]);

                                    }
                                }


                                //customerManager();

                            } else {
                                Toast.makeText(getApplicationContext(), "" + js.getString("msg"), Toast.LENGTH_SHORT).show();


                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                });
    }
}
